# -*- coding: utf-8 -*-
# Central library of sprite surfaces

import pygame
import re
import sys,os

import data

class Animation:
    def __init__(self, anim_path):
        try:
            file = open(os.path.join(data.anim_dir, anim_path, "anim"), "r")
        except IOError:
            try:
                file = open(os.path.join(data.anim_dir, anim_path, "anim.txt"), "r")
            except IOError:
                print "Invalid Animation Path:", anim_path
                sys.exit(0)
        
        self.images = []
        for line in file:
            
            word = line.split()
            if word:
                stuff = data.load_image(os.path.join(data.anim_dir,
                                                     anim_path, word[0]), -1, True)
                #image, number of frames
                self.images.append((stuff, int(word[1]))) 

        self.counter = 0
        self.imgnr = 0
        self.image, self.rect = self.images[self.imgnr][0]
        
    def next(self):
        if self.images[self.imgnr][1] == self.counter:
            self.imgnr += 1
            self.counter = 0
            #loop the animation
            if self.imgnr >= len(self.images):
                self.imgnr = 0
            return self.images[self.imgnr][0]
        else:
            #we do not need to update image
            self.counter += 1
            return None
        
